home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c / 237 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.9 KB  |  63 lines

  1. Path: taurus.fccc.edu!slifker
  2. From: slifker@castor.fccc.edu (Michael J. Slifker)
  3. Newsgroups: comp.std.c
  4. Subject: Two questions - Pointer equality and ...
  5. Date: 30 Jan 1996 19:06:50 GMT
  6. Organization: Fox Chase Cancer Center, Philadelphia, PA
  7. Message-ID: <4elq8a$j0b@taurus.fccc.edu>
  8. NNTP-Posting-Host: castor.rm.fccc.edu
  9.  
  10. Question 1:
  11.  
  12. According to K&R II, "pointer comparison is defined only for parts
  13. of the same object". In particular, this is stated to apply to 
  14. equality operators (== and !=). Harbison & Steele (_C: A Reference
  15. Manual_) agree with this in the case of relational operators, but
  16. state that pointer equality is defined provided both operands are
  17. pointers to compatible types. The latter makes much more sense to
  18. me, but can anyone tell me what the Standard has to say? 
  19.  
  20. (Oversimplifying what I want to do: I want to define a hash function
  21. on structures containing pointers, and after I determine that two
  22. structures hash to the same index, I need to see if they are identical,
  23. which requires checking that the component pointers are identical.
  24. I've programmed this, and it works (gcc on a DEC Alpha), but I'd
  25. like to clear up my confusion...)
  26.  
  27.  
  28.  
  29. Question 2:
  30.  
  31. I've compiled the following program with gcc:
  32.  
  33. -----------
  34.  
  35. #include <stdio.h>
  36.  
  37. char *str1 = "Hello, World\n";
  38. const char *str2 = "Hello, World\n";
  39.  
  40. int main(void)
  41. {
  42.   str1[0] = 'J';
  43.   printf("%s", str2);
  44.   return 0;
  45. }
  46.  
  47. -----------
  48.  
  49. ...and it prints out "Jello, World". This seems odd, at least to
  50. me. Is this a bug, or is the behavior above officially undefined?
  51.  
  52. --------------------------------------------------------
  53. Michael Slifker               E-mail: M_Slifker@fccc.edu
  54. Senior Programmer/Analyst
  55. Dept. of Biostatistics
  56. Fox-Chase Cancer Center
  57. --------------------------------------------------------
  58. -- 
  59. --------------------------------------------------------
  60. Michael Slifker               E-mail: M_Slifker@fccc.edu
  61. Senior Programmer/Analyst
  62. Dept. of Biostatistics
  63.